home *** CD-ROM | disk | FTP | other *** search
/ Language/OS - Multiplatform Resource Library / LANGUAGE OS.iso / p4 / p4-1_2b.lha / p4-1.2b / lib / p4_secure.c < prev    next >
C/C++ Source or Header  |  1993-02-06  |  6KB  |  324 lines

  1. #include "p4.h"
  2. #include "p4_sys.h"
  3.  
  4. #if defined(SYMMETRY) || defined(SUN) || \
  5.     defined(DEC5000)  || defined(SGI) || \
  6.     defined(RS6000)
  7.  
  8. /**********************************
  9. #include <sys/types.h>
  10. #include <sys/socket.h>
  11. #include <netinet/in.h>
  12. #include <netdb.h>
  13. #include <pwd.h>
  14. #include <stdio.h>
  15. **********************************/
  16.  
  17. /* #define DEBUG */
  18.  
  19. char *start_prog_error;
  20.  
  21. extern int errno;
  22. extern char *sys_errlist[];
  23.  
  24. static int connect_to_server();
  25. static void send_string();
  26. static void recv_string();
  27.  
  28. int start_slave(host, username, prog, port, am_slave, pw_hook)
  29. char *host, *username, *prog, *am_slave;
  30. int port;
  31. char *(*pw_hook) ();
  32. {
  33.     int n, conn;
  34.     struct passwd *pw;
  35.     char port_string[250];
  36.     char *pw_string;
  37.     static char buf[250];
  38.     char *local_username;
  39.     char myhost[256];
  40.     int new_port, new_fd, stdout_fd;
  41.     char msg[500];
  42.     struct sockaddr_in temp;
  43.     int rc, templen;
  44.  
  45.     myhost[0] = '\0';
  46.     get_qualified_hostname(myhost);
  47.     /* gethostname(myhost, sizeof(myhost)); */
  48.  
  49.     conn = connect_to_server(host);
  50.     if (conn < 0)
  51.     return -1;
  52.  
  53. #ifdef DEBUG
  54.     printf("Connected\n");
  55. #endif
  56.  
  57.     pw = getpwuid(geteuid());
  58.     if (pw == NULL)
  59.     {
  60.     extern char *getlogin();
  61.  
  62.     local_username = getlogin();
  63.     if (local_username == NULL)
  64.     {
  65.         start_prog_error = "Cannot get pw entry";
  66.         return -3;
  67.     }
  68.     }
  69.     else
  70.     {
  71.     local_username = pw->pw_name;
  72.     }
  73.  
  74.     send_string(conn, local_username);
  75.     send_string(conn, username);
  76.     recv_string(conn, buf, sizeof(buf));
  77. #ifdef DEBUG
  78.     printf("Got reply1 '%s'\n", buf);
  79. #endif
  80.  
  81.     if (strncmp(buf, "Password", 8) == 0)
  82.     {
  83.     if (pw_hook == NULL)
  84.         pw_string = "";
  85.     else
  86.         pw_string = (*pw_hook) (host, username);
  87.     send_string(conn, pw_string);
  88.     recv_string(conn, buf, sizeof(buf));
  89. #ifdef DEBUG
  90.     printf("Got reply '%s'\n", buf);
  91. #endif
  92.     if (strncmp(buf, "Proceed", 7) != 0)
  93.     {
  94.         start_prog_error = buf;
  95.         return -4;
  96.     }
  97.     }
  98.     else if (strncmp(buf, "Proceed", 7) != 0)
  99.     {
  100.     start_prog_error = buf;
  101.     return -4;
  102.     }
  103.  
  104.     sprintf(port_string, "%s %d %s", myhost, port, am_slave);
  105.     send_string(conn, prog);
  106.     send_string(conn, port_string);
  107.  
  108.     if (fork_p4() == 0)
  109.     {
  110.     net_setup_anon_listener(10, &new_port, &new_fd);
  111.     sprintf(port_string, "%d", new_port);
  112.     fflush(stdout);
  113.     send_string(conn, port_string);
  114.     fflush(stdout);
  115.     /* stdout_fd = net_accept(new_fd); */
  116.     templen = sizeof(temp);
  117.     SYSCALL_P4(stdout_fd, accept(new_fd, (struct sockaddr *) &temp, &templen));
  118.     close(new_fd);
  119.  
  120.     n = 1;
  121.     while (n > 0)
  122.     {
  123.         SYSCALL_P4(n, read(stdout_fd, msg, 499));
  124.         if (n > 0)
  125.         {
  126.         SYSCALL_P4(rc, write(1,msg,n));
  127.         fflush(stdout);
  128.         }
  129.     }
  130.     exit(0);
  131.     }
  132.  
  133.     recv_string(conn, buf, sizeof(buf));
  134. #ifdef DEBUG
  135.     printf("Got reply2 '%s'\n", buf);
  136. #endif
  137.     if (strncmp(buf, "Success", 7) != 0)
  138.     {
  139.     start_prog_error = buf;
  140.     return -4;
  141.     }
  142.     start_prog_error = buf;
  143.     close(conn);
  144.  
  145.     return 0;
  146. }
  147.  
  148. static int connect_to_server(host)
  149. char *host;
  150. {
  151.     int conn;
  152.     int rc;
  153.     struct hostent *hostent;
  154.     struct sockaddr_in addr;
  155.  
  156.     SYSCALL_P4(conn, socket(AF_INET, SOCK_STREAM, 0));
  157.     if (conn < 0)
  158.     {
  159.     start_prog_error = sys_errlist[errno];
  160.     return -1;
  161.     }
  162.  
  163.     hostent = gethostbyname_p4(host);
  164.  
  165.     addr.sin_family = hostent->h_addrtype;
  166.     addr.sin_port = htons(sserver_port);
  167.     bcopy(hostent->h_addr, &addr.sin_addr, hostent->h_length);
  168.  
  169.     SYSCALL_P4(rc, connect(conn, (struct sockaddr *) & addr, sizeof(addr)));
  170.     if (rc < 0)
  171.     {
  172.     start_prog_error = sys_errlist[errno];
  173.     return -1;
  174.     }
  175.  
  176.     return conn;
  177. }
  178.  
  179. static void send_string(sock, str)
  180. int sock;
  181. char *str;
  182. {
  183.     int rc, len = strlen(str);
  184.     char nl = 10;
  185.  
  186.     SYSCALL_P4(rc, write(sock, str, len));
  187.     if (rc < 0)
  188.     {
  189.     perror("write");
  190.     p4_error("send_string write 1 ", -1);
  191.     }
  192.     SYSCALL_P4(rc, write(sock, &nl, 1));
  193.     if (rc < 0)
  194.     {
  195.     perror("write");
  196.     p4_error("send_string write 2 ", -1);
  197.     }
  198.  
  199. }
  200.  
  201. static void recv_string(sock, buf, len)
  202. int sock, len;
  203. char *buf;
  204. {
  205.     char *bptr;
  206.     int n;
  207.  
  208.     bptr = buf;
  209.     while (1)
  210.     {
  211.     SYSCALL_P4(n, read(sock, bptr, 1));
  212.     if (n < 0)
  213.     {
  214.         perror("read");
  215.         p4_error("recv_string read ", -1);
  216.         exit(1);
  217.     }
  218.     if (*bptr == '\n')
  219.         break;
  220.     bptr++;
  221.     if (bptr - buf >= len)
  222.         break;
  223.     }
  224.     *bptr = 0;
  225. }
  226.  
  227. #ifdef P4BSD
  228.  
  229. #include <sys/ioctl.h>
  230. static struct sgttyb orig_tty;
  231.  
  232. static int echo_off()
  233. {
  234.     struct sgttyb tty_new;
  235.  
  236.     if (ioctl(0, TIOCGETP, &orig_tty) < 0)
  237.     {
  238.     fprintf(stderr, "iotcl TIOCGETP failed: %s\n", sys_errlist[errno]);
  239.     return -1;
  240.     }
  241.  
  242.     tty_new = orig_tty;
  243.     tty_new.sg_flags &= ~(ECHO);
  244.  
  245.     if (ioctl(0, TIOCSETP, &tty_new) < 0)
  246.     {
  247.     fprintf(stderr, "iotcl TIOCSETP failed: %s\n", sys_errlist[errno]);
  248.     return -1;
  249.     }
  250. }
  251.  
  252. static int echo_on()
  253. {
  254.     if (ioctl(0, TIOCSETP, &orig_tty) < 0)
  255.     {
  256.     fprintf(stderr, "iotcl TIOCSETP failed: %s\n", sys_errlist[errno]);
  257.     return -1;
  258.     }
  259. }
  260.  
  261. #else
  262.  
  263. #include <termio.h>
  264.  
  265. struct termio tty_orig;
  266.  
  267. static int echo_off()
  268. {
  269.     struct termio tty_new;
  270.  
  271.     if (ioctl(0, TCGETA, &tty_orig) < 0)
  272.     {
  273.     fprintf(stderr, "tcgetattr failed: %s\n", sys_errlist[errno]);
  274.     return -1;
  275.     }
  276.  
  277.     tty_new = tty_orig;
  278.  
  279.     tty_new.c_lflag &= ~(ECHO);
  280.  
  281.     if (ioctl(0, TCSETA, &tty_new) < 0)
  282.     {
  283.     fprintf(stderr, "tcsetattr failed: %s\n", sys_errlist[errno]);
  284.     return -1;
  285.     }
  286. }
  287.  
  288. static int echo_on()
  289. {
  290.     if (ioctl(0, TCSETA, &tty_orig) < 0)
  291.     {
  292.     fprintf(stderr, "tcsetattr failed: %s\n", sys_errlist[errno]);
  293.     return -1;
  294.     }
  295. }
  296.  
  297. #endif
  298.  
  299. char *getpw(host, name)
  300. char *host, *name;
  301. {
  302.     static char buf[1024];
  303.     char *s;
  304.  
  305.     echo_off();
  306.     printf("Password for %s@%s: ", name, host);
  307.     fflush(stdout);
  308.     fgets(buf, sizeof(buf), stdin);
  309.     echo_on();
  310.     printf("\n");
  311.  
  312.     for (s = buf; *s; s++)
  313.     if (*s == '\n')
  314.     {
  315.         *s = 0;
  316.         break;
  317.     }
  318.  
  319.     return buf;
  320. }
  321.  
  322. #endif
  323. /* #ifdef SYMMETRY */
  324.